﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Furion.DependencyInjection;
using MyFurion.WFSqlsugar.Model;
using MyFurion.WFSqlsugar.Application.Dtos;
using SqlSugar;
using System.Linq.Expressions;

namespace MyFurion.WFSqlsugar.Application
{
    /// <summary>
    /// 用户仓储业务
    /// </summary>
    public class UserRepository : BaseRepository<SystemUser>, ITransient
    {
        /// <summary>
        /// 账户是否已存在
        /// </summary>
        /// <param name="account"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<bool> IsExists(string account, long id)
        {
            return await Context.Queryable<SystemUser>().AnyAsync(x => x.Account == account && x.Id != id);
        }
        /// <summary>
        /// 分页数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PageResult<SystemUser>> GetPageList(PageInputBase input)
        {
            var exp = Expressionable.Create<SystemUser>();
            exp.AndIF(!string.IsNullOrWhiteSpace(input.SearchValue), it => it.UserName.Contains(input.SearchValue)||it.Account.Contains(input.SearchValue));
            Expression<Func<SystemUser, dynamic>> sortPredicate = x => x.CreateTime;
            return await GetPageListAsync(exp.ToExpression(),input.PageNo,input.PageSize, sortPredicate);
        }
    }
}
